namespace :limit do
  # rake limit:list
  desc "list"
  task list: :environment do
    last_trade_date = LimitList.last.trade_date
    limit_lists = LimitList.where(trade_date: last_trade_date)
    puts "#{last_trade_date}  ----- 涨停 #{limit_lists.count} 只"
    limit_arr = Parallel.map(limit_lists, in_processes: 4, progress: true) do |limit|
      limit_hash = {}
      today = JSON.parse $Redis.get(limit.ts_code[0..5])
      (puts "没有 #{limit.ts_code[0..5]}的实时数据"; next) if today.blank?
      # today_volume = trade_time || Time.now.hour > 15 ? today_volume(limit.ts_code[0..5]) : 100
      limit_hash[:name] = today["name"]
      limit_hash[:code] = today["code"]
      limit_hash[:today_turn_ratio] = today["turnoverratio"].to_f.round(1) # 当前换手
      limit_hash[:settlement] = today["settlement"] # 前日收盘
      limit_hash[:high] = today["high"] # 最高
      limit_hash[:change] = today["changepercent"].to_f.round(1)
      limit_hash[:open_change] = cul_change(today["open"], today["trade"])
      limit_hash[:limit_count] = limit.year_count
      limit_hash[:limit_times] = limit.limit_times
      limit_hash[:turnover_ratio] = limit.turnover_ratio.to_f.round(1) # 昨日换手率
      limit_hash[:industry] = limit.industry
      limit_hash[:float_mv] = (limit.float_mv / 100000000).round(0)
      limit_hash[:total_mv] = (limit.total_mv / 100000000).round(0)
      limit_hash[:first_time] = limit.first_time # 首次封板时间
      limit_hash[:last_time] = limit.last_time # 最终封板时间
      limit_hash[:open_times] = limit.open_times # 炸板次数
      limit_hash[:ths_flow] = limit.ths_flow
      limit_hash[:score] = limit.score
      limit_hash[:today_volume] = "today_volume"
      limit_hash
    end

    puts "大于2板  看涨幅、开盘涨，高位连板。大涨，连板，板块情绪高，行情持续"
    limit_arr.compact.select { |x| x[:limit_times] > 2 }.sort_by { |x| x[:limit_times] }.reverse.each do |limit|
      printf_limit_flow limit
    end
    puts "1板、2板 看涨幅、开盘涨，高位连板。大涨，连板，板块情绪高，行情持续"
    limit_arr.compact.select { |x| x[:limit_times] <= 2 }.sort_by { |x| x[:industry] }.each do |limit|
      printf_limit_flow limit
    end

    puts "今日涨幅 大于 7%"
    limit_arr.compact.select { |x| x[:change] >= 7 }.sort_by { |x| x[:industry] }.each do |limit|
      printf_limit_flow limit
    end
    puts "今日涨幅 大于 3%< 7%"
    limit_arr.compact.select { |x| x[:change] >= 3 && x[:change] < 7}.sort_by { |x| x[:change] }.each do |limit|
      printf_limit_flow limit
    end
    puts "今日涨幅 大于 0%< 3%"
    limit_arr.compact.select { |x| x[:change] >= 0 && x[:change] < 3}.sort_by { |x| x[:change] }.each do |limit|
      printf_limit_flow limit
    end
    puts "今日涨幅 小于 0%"
    limit_arr.compact.select { |x| x[:change] < 0 }.sort_by { |x| x[:change] }.each do |limit|
      printf_limit_flow limit
    end
    limit_arr.group_by { |x| x[:industry] }.sort_by { |industry, items| -items.count }.each do |industry, items|
      next if items.count < 2
      puts "#{industry} -- #{items.count}"
      items.each do |item|
        printf_limit_flow item
      end
    end
  end
  # rake limit:today_list
  desc "today_list"
  task today_list: :environment do
    # TODO 根据所有股票实时价格找出当日涨停的股票分析
  end
end

def printf_limit_flow(limit)
  printf("[量:%5s 换手:%5s] \e[31m涨停\e[0m:%2s 连板:%2s||流/市：%4s/%4s||封板%5s-%5s 炸板:%2s S:%3s F:%6s %5s %5s %6s 涨幅：%-5s 开涨：%-5s  \n",
         limit[:today_volume], limit[:today_turn_ratio],
         limit[:limit_count], limit[:limit_times],
         limit[:float_mv], limit[:total_mv], "#{limit[:first_time][-6..-5]}:#{limit[:first_time][-4..-3]}", "#{limit[:last_time][-6..-5]}:#{limit[:last_time][-4..-3]}", limit[:open_times],
         limit[:score], limit[:ths_flow].to_i,
         limit[:industry][0..3].ljust(4, padstr = "囗"), limit[:name].gsub(" ", "").ljust(4, padstr = "囗"), limit[:code], auto_p_color_n(limit[:change]), auto_p_color_n(limit[:open_change]))
end
